package com.ace.tlog

import com.ace.log.file.name.IFileNameGenerator
import java.text.SimpleDateFormat
import java.util.*

/**
 * 每隔一定的小时数对日志进行分片
 */
class DateWithSecondFileNameGenerator(val intervalSeconds: Int) : IFileNameGenerator {

    private val mLocalDateFormat: ThreadLocal<SimpleDateFormat> =
        object : ThreadLocal<SimpleDateFormat>() {
            override fun initialValue() = SimpleDateFormat("yyyy-MM-dd-HH-mm", Locale.CHINA)
        }

    override fun isFileNameChangeable() = true

    override fun generateFileName(
        logLevel: Int,
        tag: String,
        timestamp: Long,
        lastFileName: String
    ): String {
        val sdf = mLocalDateFormat.get() ?: SimpleDateFormat("yyyy-MM-dd-HH-mm", Locale.CHINA)

        return sdf.let {
            it.timeZone = TimeZone.getDefault()
            val date = Date(timestamp)
            val dateString = it.format(date)
            val seconds = (timestamp / 1000) % 60  // 获取秒数
            "$dateString-${seconds / intervalSeconds}"
        }
    }
}